package ru.susu.algebra.centralunits.alternating.tex;

import ru.susu.algebra.centralunits.alternating.MathMethodWithInitializers;
import ru.susu.algebra.centralunits.alternating.initializers.SpecialRowsInitializer;
import ru.susu.algebra.chartable.constructor.AlternatingCharTableUtil;
import ru.susu.algebra.field.quadratic.QuadraticField;
import ru.susu.algebra.jtex.ITexElement;
import ru.susu.algebra.jtex.SimpleTexElementWithCode;
import ru.susu.algebra.jtex.StringTexElement;
import ru.susu.algebra.jtex.TexBeginEndElement;
import ru.susu.algebra.jtex.UnionTexElement;
import ru.susu.algebra.jtex.formula.FormulaTexElement;
import ru.susu.algebra.jtex.formula.GreekSymbols;
import ru.susu.algebra.jtex.utils.TexUtils;
import ru.susu.algebra.partition.Partition;
import ru.susu.algebra.properties.IPropertySource;

/**
 * @author akargapolov
 * @since: 26.08.2010
 */
public class CharacterFieldLemmasGenerator extends MathMethodWithInitializers<ITexElement>
{
	private static final Class[] DEPENDENCIES = {SpecialRowsInitializer.class};

	public static String getLemmaName(Integer rowNumber)
	{
		return "uField" + rowNumber;
	}

	@Override
	protected ITexElement directRun(IPropertySource ps) throws Exception
	{
		UnionTexElement union = new UnionTexElement();
		union.addSubElement(StringTexElement.comment("леммы о полях особых характеров"));
		for (Partition specialRow : SpecialRowsInitializer.listSpecialNotIntegerCharacterRows(ps))
		{
			Integer number = SpecialRowsInitializer.getRowNumber(ps, specialRow);

			TexBeginEndElement lemma = TexBeginEndElement.lemma();
			lemma.addSubElement(SimpleTexElementWithCode.label(getLemmaName(number)));
			QuadraticField quadraticField = AlternatingCharTableUtil.getQuadraticField4SpecialRow(specialRow);
			String sqrtNumber = FormulaTexElement.sqrt(quadraticField.getGenerateInteger().getD().toString());
			String qSqrtNumber = " \\Q\\left(" + sqrtNumber + "\\right)";
			String omegaNumber = GreekSymbols.OMEGA + TexUtils.index(quadraticField.getGenerateInteger().getD());
			String generalUnit = TexUtils.toTexString(quadraticField.getGeneralUnit());

			lemma.addSubElement(StringTexElement.text("Поле характера $\\Q\\left(" + GreekSymbols.CHI + SpecialRowsInitializer.getIndexByRowNumber(ps, specialRow) +
					" \\right) = " + qSqrtNumber + "$, кольцо целых поля $" + qSqrtNumber + "$ равно $\\Z[\\omega]$, где \n " +
					"$" + omegaNumber + " = " + FormulaTexElement.frac("1+" + sqrtNumber, "2") + "$ и группа единиц \n" +
					"\\[\n" +
					"\\Un\\left(\\Z[" + GreekSymbols.OMEGA + "]\\right) = " +
					FormulaTexElement.LANGLE + "-1" + FormulaTexElement.RANGLE + FormulaTexElement.TIMES +
					FormulaTexElement.LANGLE + generalUnit + FormulaTexElement.RANGLE + "\n" +
					"\\]\n" +
					"Поэтому\n"));
			lemma.addSubElement(TexBeginEndElement.equation().addSubElement(
					StringTexElement.text(GreekSymbols.LAMBDA + "=" + FormulaTexElement.VAREPSILON + "\\left(" + generalUnit + "\\right)^k,")));
			lemma.addSubElement(StringTexElement.text("где $" + FormulaTexElement.VAREPSILON + FormulaTexElement.IN + "\\{1,-1\\}$ и $k\\in\\Z$."));
			union.addSubElement(lemma);
		}

		union.addSubElement(StringTexElement.newLineDouble());
		return union;
	}

	@Override
	protected Class[] getDependentInitializers()
	{
		return DEPENDENCIES;
	}

}
